package com.xuce.nzshop.protal.controller;

import com.google.gson.Gson;
import com.xuce.constant.CookieConstant;
import com.xuce.constant.RedisConstant;
import com.xuce.dto.ResultBean;
import com.xuce.pojo.TUser;
import com.xuce.util.HttpClientUtils;
import com.xuce.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    RedisTemplate redisTemplate;

//    private IProductTypeService productTypeService;


    @RequestMapping({"","index"})
    public String showIndex(Model model) {

//        List<TProductType> types = productTypeService.selectAll();
//        model.addAttribute("types",types);
        return "index";
    }

    @RequestMapping("checkIsLogin")
    @ResponseBody
    // HttpServletRequest对象中获取Cookie的键是“user_login”的那个值，赋值给uuid,根据uuid组织好redis中键 直接访问redis
    public ResultBean checkIsLogin(@CookieValue(name= CookieConstant.USER_LOGIN,required = false)String uuid){

//        //来访问sso提供的checkIsLogin接口==》得到resultBean
//        //1.使用HttpClient工具类来发送请求
//        String url = "http://localhost:9083/user/checkIsLogin";
//        //dbe06afc-8540-4b74-8035-99e188d33933
//        String cookie = new StringBuilder().append(CookieConstant.USER_LOGIN).append("=").append(uuid).toString();
//        // user_login=dbe06afc-8540-4b74-8035-99e188d33933
//        String result = HttpClientUtils.doGet(url, cookie); //result是一个json字符串
//
//        //把result字符串封装成ResultBean对象
//        //jackjson
//        Gson gson = new Gson();
//        ResultBean resultBean = gson.fromJson(result, ResultBean.class);
//
//        System.out.println("protal uuid :"+uuid);
//        System.out.println("protal resultBean : "+resultBean.getData());
//        return resultBean;
        System.out.println("protal uuid :"+uuid);
        if(uuid!=null&&!"".equals(uuid)){
            //1.组织键   user:login:dbe06afc-8540-4b74-8035-99e188d33933
            String redisKey = StringUtil.getRedisKey(RedisConstant.USER_LOGIN_PRE, uuid);
            //2.去redis中查
            Object o = redisTemplate.opsForValue().get(redisKey);
//            Gson gson = new Gson();
//            ResultBean resultBean = gson.fromJson(o.toString(), ResultBean.class);
//            System.out.println("protal resultBean data "+resultBean.getData());
            System.out.println("protal redisKey: "+redisKey);
            System.out.println("protal Object: "+o);
            if(o!=null){
//                TUser user  = (TUser) o;
//                user.setPassword("");//数据传递时不带密码
                LinkedHashMap user = (LinkedHashMap)o;
                Iterator it = user.entrySet().iterator();
                while (it.hasNext()){
                    Map.Entry entry = (Map.Entry) it.next();
                    System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
                }
                return ResultBean.success(user,"用户已登录");
            }
        }
        return ResultBean.error("用户未登录");
    }



}
